package cn.com.service.impl;

import cn.com.Util.ToolUtil;
import cn.com.mapper.FilesMapper;
import cn.com.service.FileRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.com.domain.FileRelationEntity;
import cn.com.domain.FilesEntity;
import cn.com.mapper.FileRelationMapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author CRL
 * @date 2021/4/20 13:15
 */
@Service
public class FileRelationServiceImpl extends ServiceImpl<FileRelationMapper, FileRelationEntity> implements FileRelationService {

    @Autowired
    FileRelationMapper fileRelationMapper;
    @Autowired
    FilesMapper filesMapper;

    @Override
    public void deleteByObjId(String id) {
        //删除文件数据关联系
        Map<String, Object> queryParams = new HashMap<>();
        queryParams.put("obj_id", id);
        this.fileRelationMapper.deleteByMap(queryParams);
    }

    @Override
    public List<FilesEntity> findFilesByObjId(String objId) {
        //删除文件数据关联系
        Map<String, Object> queryParams = new HashMap<>();
        queryParams.put("obj_id", objId);
        List<FileRelationEntity> fileRelationEntities = fileRelationMapper.selectByMap(queryParams);
        if (ToolUtil.isNotEmpty(fileRelationEntities)) {
            List<String> fileIds = fileRelationEntities.stream().map(FileRelationEntity::getFileId).collect(Collectors.toList());
            return filesMapper.selectBatchIds(fileIds);
        } else {
            return null;
        }

    }


}
